#This is to get
#Figure4
rm(list = ls())
library("rgdal")
library("ggplot2")
library("raster")
library("lemon")
library("ggsn")
library("raster")


setwd("C:/Users/bogdanp/Dropbox/Legacies_Central_Europe/")
setwd("/Users/bgpopescu/Dropbox/Legacies_Central_Europe/")




###########################
#Processing Data by vilayet#
###########################

#Reading polygons
romania_empires <- readOGR(dsn="./data/shape_files.gdb",
                           layer="romania", use_iconv = TRUE, encoding = "UTF-8")


#Reading rasters
ro_raster <- raster("./data/rasters/romania_raster.tif")

#Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(ro_raster); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")

#   Create vectors for colour breaks
b.hs <- seq(min(hdf$Hill),max(hdf$Hill),length.out=100)

##############################
#MAP 1 General Habsburg Lands#
##############################
romania_empires_fort<-fortify(romania_empires)

map<-ggplot()+
  #Raster works but takes a long time
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude", guide = "none")  + 
  geom_polygon(data = romania_empires_fort,  
               aes(x=long, y=lat, group = group, color = id, fill = id), size = 0.6)+
  scale_fill_manual(values = alpha(c("blue", "pink"), alpha =.5),
                    name="Territory",
                    labels=c("Ottoman", "Habsburg"))+
  scale_colour_manual(values=c("black", "black"), guide = "none")+
  coord_cartesian(ylim=c(42.619140,49.261440), xlim=c(20.263540, 29.688470))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  ggtitle("")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        #Legend.position values should be between 0 and 1. c(0,0) corresponds to the "bottom left"
        #and c(1,1) corresponds to the "top right" position.
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 29, x.max = 29.5,
                 #Above you mention how long the scalebar should be
                 y.min = 48.6, y.max = 49,
                 #Above you mention how thick the scalebar should be
                 dist = 80, dist_unit = "km",
                 transform = T, model = "WGS84",
                 location = "topright",
                 st.size = 3,
                 #Above you have the font size of the numbers below the scalebar
                 st.dist =0.3,
                 #Above you have the distance between the bar and the text, as a proportion of the y axis.
                 height=0.2)
# #Above you have anumber between 0 and 1 to indicate the height 
# #of the scale bar, as a proportion of the y axis

map<-reposition_legend(map, 'bottom left')

ggsave(map,file="./Paper/graphs/figure4.jpg", height=19.42, width=20.2, units = "cm", dpi=300)


